package com.catmiao.rdd.operate.transform;

import com.google.common.collect.Lists;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import scala.Tuple2;

import java.util.ArrayList;

/**
 * @author ChengMiao
 * @title: Transfer_01_Map
 * @projectName spark_study
 * @description: TODO
 * @date 2024/11/25 16:27
 */
public class Transfer_10_kv {

    public static void main(String[] args) throws InterruptedException {

        final SparkConf conf = new SparkConf();
        conf.setAppName("appName");
        conf.setMaster("local[*]");

        final JavaSparkContext jsc = new JavaSparkContext(conf);

        final Tuple2<String,Integer> a = new Tuple2<>("a",1);
        final Tuple2<String,Integer> a1 = new Tuple2<>("b",2);
        final Tuple2<String,Integer> a2 = new Tuple2<>("c",3);


        ArrayList<Tuple2<String, Integer>> tuple2s = Lists.newArrayList(a, a1, a2);

//        JavaRDD<Tuple2<String, Integer>> rdd = jsc.parallelize(tuple2s);
//
//
//        JavaRDD<Tuple2<String, Integer>> rdd2 = rdd.map(
//                t -> new Tuple2<>(t._1, t._2 * 2)
//        );
//
//        rdd2.collect().forEach(System.out::println);

        // 以上并不是 kv类型  还是单值类型，而是将2元祖当成一个整体来使用

        JavaPairRDD<String, Integer> pairRDD = jsc.parallelizePairs(tuple2s);


        // TODO mapValues只对V进行处理，K不做任何操作
        pairRDD.mapValues(
                num -> num *2
        ).collect().forEach(System.out::println);


        jsc.close();


    }
}
